package com.zjnx.train.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.zjnx.train.dao.UserDao;
import com.zjnx.train.entity.User;

/**
 * @author foo
 * @email foo@zjnx.com
 * @description:dao接口实现
 */
@Repository
public class UserDaoImpl implements UserDao {

	@Autowired
	SessionFactory sessionFactory;

	private Session currentSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	public void save(User user) {
		currentSession().save(user);
	}

	@Override
	public void update(User user) {
		currentSession().update(user);
	}

	@Override
	public void delete(Long id) {
		User user = findByKey(id);
		if (user != null) {
			currentSession().delete(user);
		}
	}

	@Override
	public User findByKey(Long id) {
		return (User) currentSession().get(User.class, id);
	}

	@Override
	public List<User> findAll() {
		Criteria query = currentSession().createCriteria(User.class);
		@SuppressWarnings("unchecked")
		List<User> users = query.list();
		return users;
	}

}
